package com.feihong.txgw.tx_precenter;

import com.alibaba.fastjson.JSON;
import com.feihong.txgw.inter.contract.login.ILoginContract;
import com.feihong.txgw.model.bean.deliveryAdr.DeliveryAdrEntity;
import com.feihong.txgw.tx_model.LoginMainModel;
import com.feihong.txgw.ui.activity.user.MyDeliveryAdrActivity;
import com.feihong.txgw.utils.PreferenceTools;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName LoginMainPrecenter
 * @package com.feihong.txgw.tx_precenter
 * @data 2018/5/7 18:47
 * @Describe
 * @Change
 */
public class LoginMainPrecenter extends ILoginContract.AbLoginMainPrecenter {

    public static LoginMainPrecenter create(){
        return new LoginMainPrecenter();
    }

    @Override
    public void getDeliverAdr() {
        mIModel.getDeliverAdr().subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(String result) {
                try {
                    List<DeliveryAdrEntity> mDeliveryAdrEntityList = new ArrayList<DeliveryAdrEntity>();
                    org.json.JSONObject jsonObject = new org.json.JSONObject(result);
                    if ("200".equals(jsonObject.optString("code"))) {
                        JSONArray jsonArray = jsonObject.optJSONArray("data");
                        if (jsonArray != null) {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                org.json.JSONObject deliveryAdrEntity = jsonArray.getJSONObject(i);
                                DeliveryAdrEntity deliveryAdr = JSON.parseObject(deliveryAdrEntity.toString(), DeliveryAdrEntity.class);
                                mDeliveryAdrEntityList.add(deliveryAdr);
                            }
                        }
                        boolean hasDefaultAdr = false;
                        for (DeliveryAdrEntity deliveryAdrEntity : mDeliveryAdrEntityList) {
                            if ("1".equals(deliveryAdrEntity.getIs_default())) {
                                PreferenceTools.putString(MyDeliveryAdrActivity.DEFAULT_ADR_KEY, JSON.toJSONString(deliveryAdrEntity));
                                hasDefaultAdr = true;
                                break;
                            }
                        }
                        if (!hasDefaultAdr && mDeliveryAdrEntityList.size() > 0) {
                            PreferenceTools.putString(MyDeliveryAdrActivity.DEFAULT_ADR_KEY, JSON.toJSONString(mDeliveryAdrEntityList.get(0)));
                        }
                    } else if ("5007".equals(jsonObject.optString("code"))) {
                        PreferenceTools.putString(MyDeliveryAdrActivity.DEFAULT_ADR_KEY, "");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getTelCode(String telNum, String type) {
        mIModel.getTelCode(telNum,type).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(String s) {
                mIView.getTelCodeResult(s);
            }

            @Override
            public void onError(Throwable e) {
                mIView.getTelCodeError();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void userLogin(String telNum, String code) {
        mIModel.userLogin(telNum, code).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(String s) {
                mIView.userLoginResult(s);
            }

            @Override
            public void onError(Throwable e) {
                mIView.usrLoginError();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public ILoginContract.ILoginMainModel getModel() {
        return LoginMainModel.create();
    }

    @Override
    public void onStart() {

    }
}
